package com.tengju.bff.interfaces.manage.crm.activity;

import com.tengju.bff.interfaces.manage.common.facade.CommonManageFacade;
import com.tengju.bff.interfaces.manage.crm.activity.facade.ActivityTargetAnalysisFacade;
import com.tengju.bff.interfaces.manage.crm.activity.form.*;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.TableVO;
import com.tengju.bff.interfaces.shared.render.FunnelVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.data.domain.model.clickhouse.crm.UserRole;
import com.tengju.export.application.ExportService;
import com.tengju.export.domain.record.ExportDataType;
import com.tengju.export.domain.record.ExportFileType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author chenhang
 * @Date 2021/10/13
 */
@Validated
@RestController
@Api(tags = "生意参谋-销售活动分析-总监视角")
@RequestMapping(value = "manage/crm/activity/analysis/director")
@Slf4j
@ManageEntrance
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ActivityDirectorAnalysisController {

    private final ActivityTargetAnalysisFacade activityTargetAnalysisFacade;
    private final ExportService exportService;
    private final CommonManageFacade commonManageFacade;

    /******************************           活动查看                *********************************/

    /******************************           （总监视角）活动查看         *********************************/

    @ApiOperation(value = "（总监）活动目标概览", notes = "销售目标概览")
    @RequestMapping(value = "/overview", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> overview(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.overview(query));
    }

    @ApiOperation(value = "（总监）活动目标table", notes = "销售目标table")
    @RequestMapping(value = "/targetTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> targetTable(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.overviewTable(query));
    }

    @ApiOperation(value = "（总监）活动转化漏斗", notes = "销售目标table")
    @RequestMapping(value = "/transformFunnel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> transformFunnel(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.transformFunnel(query));
    }

    @ApiOperation(value = "部门排行榜", notes = "销售目标table")
    @RequestMapping(value = "/departmentRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> departmentRankTable(@RequestBody @Valid ActivityDirectorQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.departmentRankTable(query));
    }

    @ApiOperation(value = "SEC排行榜", notes = "销售目标table")
    @RequestMapping(value = "/secRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> secRankTable(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.secRankTable(query));
    }

    @ApiOperation(value = "总监排行榜", notes = "销售目标table")
    @RequestMapping(value = "/directorRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> directorRankTable(@RequestBody @Valid ActivityDirectorQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.directorRankTable(query));
    }

    @ApiOperation(value = "加购商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderAddcartGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderAddcartGoodsRankTable(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.addcartGoodsRankTable(query));
    }

    @ApiOperation(value = "出货商品排行榜", notes = "销售目标table")
    @RequestMapping(value = "/serviceProviderBuyGoodsRankTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> serviceProviderBuyGoodsRankTable(@RequestBody @Valid ActivityBaseQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.moneyoutRankTable(query));
    }



    /******************************        目标达成   *********************************/



    @ApiOperation(value = "总监目标达成", notes = "总监目标达成")
    @RequestMapping(value = "/directorTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<TableVO> directorTargetReachTable(@RequestBody ActivityDirectorQuery query) {
        return ApiResponse.newSuccess(activityTargetAnalysisFacade.directorTargetReachTable(query));
    }

    /******************************           目标达成  下载               *********************************/


    @ApiOperation(value = "下载总监目标达成", notes = "下载总监目标达成")
    @RequestMapping(value = "/exportDirectorTargetReachTable", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<Object> exportDirectorTargetReachTable(@RequestBody @Valid ActivityDirectorQuery query) {
        long staffId = ServletContextUtil.getServletContext().getStaffId().getValue();
        String realName = ServletContextUtil.getServletContext().getRealName();
        exportService.exportData("总监目标达成数据导出", ExportFileType.XLSX,
                "directorTargetReachExportDataSourceRepository",
                ExportDataType.TABLE, query,
                staffId, realName);
        return ApiResponse.newSuccess();
    }
}
